package com.tjl.warehouse.controller;

import com.tjl.warehouse.annotation.AuthToken;
import com.tjl.warehouse.common.ErrorCode;
import com.tjl.warehouse.common.R;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.model.InStore;
import com.tjl.warehouse.model.MyPage;
import com.tjl.warehouse.model.Store;
import com.tjl.warehouse.model.dto.InStoreReq;
import com.tjl.warehouse.service.InStoreService;
import com.tjl.warehouse.service.StoreService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Tang
 */
@RestController
@Slf4j
@Tag(name = "入库接口")
@AuthToken
@RequestMapping("/instore")
public class InStoreController {

    @Resource
    StoreService storeService;
    @Resource
    InStoreService inStoreService;

    //获取入库记录
    @GetMapping("/instore-page-list")
    public R<MyPage<InStore>> getInStoreList(InStoreReq req, MyPage<InStore> page) {
        //检查参数
        ThrowUtils.throwIfParams(page == null);
        ThrowUtils.throwIfParams(page.getPageNum() <= 0, "页码最小为1");
        ThrowUtils.throwIfParams(page.getPageSize() >= 500, "最大每页500条");

        MyPage<InStore> pageList = inStoreService.getPageList(req, page);
        return R.success(pageList);
    }


    //获取仓库
    @GetMapping("/store-list")
    public R<List<Store>> getStoreList() {
        return R.success(storeService.list());
    }

    @PutMapping("/instore-confirm")
    public R<Boolean> confirm(@RequestBody InStoreReq req) {

        if (req == null || req.getInsId() <= 0){
            return R.error(ErrorCode.PARAMS_ERROR);
        }
        InStore inStore = new InStore();
        inStore.setIsIn("1");
        inStore.setInsId(req.getInsId());
        boolean save = inStoreService.updateById(inStore);
        if (save){
            return R.success(true);
        }else{
            return R.error(false);
        }
    }


}
